/**
 * @author wn
 * @date 2022/10/13 10:51:54
 * @description: 公共数据  共享  模块
 */

import { getAsideList } from '@/network/constants'

export default {
	namespaced: true,
	state() {
		return {
			// 是否折叠 左侧 menu collapse -- mode 为 vertical 时可用
			isCollapse: false,
			// AppHeader 面包屑
			breadcrumb: null,
			// Layout tags
			tabsList: [
				{
					path: '/home',
					name: 'home',
					label: '首页',
					icon: 'Home',
				},
			],
			// 侧边 菜单 动态路由
			menuList: null,
			// 纯 动态路由 列表
			routeList: null,
		}
	},
	mutations: {
		setCollapse(state) {
			state.isCollapse = !state.isCollapse
		},
		setBreadcrumb(state, payLoad) {
			state.breadcrumb = payLoad?.label === '首页' ? null : payLoad
		},
		setTabsList(state, payLoad) {
			// 数组中不存在  追加  已经存在 什么也不干
			const item = state.tabsList.find(item => item.name === payLoad.name)
			item || state.tabsList.push(payLoad)
		},
		CloseTab(state, payLoad) {
			state.tabsList.splice(state.tabsList.indexOf(payLoad), 1)
		},
		// 左侧 菜单 menu
		setMenuList(state, payLoad) {
			state.menuList = payLoad
		},
		clearMenuList(state) {
			state.menuList = null
		},
		// 所有动态路由
		setRouteList(state, payLoad) {
			state.routeList = payLoad
		},
	},
	actions: {
		// beforeEach  时 获取 menuList
		async getMenuList({ rootState, commit }) {
			// 获取 MenuList  API
			const { result } = await getAsideList()
			rootState.user.user?.name === '管理员' &&
				result.splice(1, 1, {
					path: '/admin',
					name: 'admin',
					label: '管理员账号',
					icon: 'User',
					url: '/user',
				})
			commit('setMenuList', result)
			return result
		},
	},
}
